package com.hongsheng.controller;

/*
@Author：郭振文
@Package：com.hongsheng.controller
@File：AuthorityController
@Date：2023/8/11 11:33
*/

import com.github.pagehelper.PageInfo;
import com.hongsheng.pojo.Authority;
import com.hongsheng.pojo.R;
import com.hongsheng.service.AuthorityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/authority")
public class AuthorityController {

    @Autowired
    private AuthorityService authorityService;

    //添加权限
    @PostMapping
    public R insertAuthority(@RequestBody Authority authority){
        if (authority.getAuthorityName()==null || authority.getAuthorityName().isEmpty()){
            return new R(false);
        }else if(authority.getFeatures()==null || authority.getFeatures().isEmpty()) {
            return new R(false);
        } else {
            Boolean flag = authorityService.insertAuthority(authority);
            return new R(flag);
        }
    }

    //删除权限
    @DeleteMapping("{id}")
    public R deleteAuthority(@PathVariable Integer id){
        Boolean flag = authorityService.deleteAuthority(id);
        return new R(flag);
    }

    //通过id查询权限
    @GetMapping("{id}")
    public R selectAuthorityById(@PathVariable Integer id){
        Authority authority = authorityService.selectAuthorityById(id);
        return new R(true,authority);
    }

    //修改权限
    @PutMapping
    public R updateAuthority(@RequestBody Authority authority) {
        if (authority.getAuthorityName() == null || authority.getAuthorityName().isEmpty()) {
            return new R(false);
        } else if (authority.getFeatures() == null || authority.getFeatures().isEmpty()) {
            return new R(false);
        } else {
            Boolean flag = authorityService.updateAuthority(authority);
            return new R(flag);
        }
    }

    //分页条件查询权限
    @GetMapping("{currentPage}/{pageSize}")
    public R selectAuthority(@PathVariable Integer currentPage, @PathVariable Integer pageSize, Authority authority){
        PageInfo<Authority> authorityList = authorityService.selectAuthority(currentPage,pageSize,authority);
        return new R(true,authorityList);
    }

    //查询所有权限
    @GetMapping
    public R selectAuthority1(){
        List<Authority> authorityList = authorityService.selectAuthority1();
        return new R(true,authorityList);
    }



}
